package ls.test;

import ls.dao.CourseDAO;
import ls.dao.DAOFactory;
import ls.dao.EvaluateDAO;
import ls.dao.StudentDAO;
import ls.entity.Course;
import ls.entity.Evaluate;
import ls.entity.Student;

import java.util.List;
import java.util.Scanner;

public class ChooseCourse {
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
    EvaluateDAO evaluateDAO = DAOFactory.getInstance().getEvaluateDAO();
    public int chooseCourse(String student_id)
    {
        Student student = studentDAO.getStudent(student_id);
        System.out.print("研究生"+student.getStudent_name()+"您好！查询到您的助教状态为：");
        String nowState = student.getState();
        System.out.println(nowState);
        if(nowState.equals("未助教"))
        {
            int flag = 0;
            if(student.getFirstVol().equals("null")&& !student.getSecondVol().equals("null")) {
                flag = 1; // 志愿一未填
            }
            else if(!student.getFirstVol().equals("null") && student.getSecondVol().equals("null"))
            {
                flag = 2;// 志愿二未填
            }
            else if(student.getFirstVol().equals("null")&&student.getSecondVol().equals("null"))
            {
                flag = 3;  // 两个都没填
            }
            else if(!student.getFirstVol().equals("null") && !student.getSecondVol().equals("null"))
            {
                flag = 4;  // 两个都填过了
            }

            if(flag == 4)   // 两个志愿都填过了,想修改志愿
            {
                System.out.println("检测到您已经填报两个志愿！");
                System.out.println("您的志愿一为：" +student.getFirstVol());
                System.out.println("您的志愿二为：" +student.getSecondVol());
                System.out.println("请选择您的操作");
                System.out.println("1.修改志愿");
                System.out.println("2.查看所报志愿课程的详细信息");
                System.out.println("3.退出系统");
                Scanner scanner = new Scanner(System.in);
                Student updateStu = new Student();
                int choose = scanner.nextInt();
                if(choose == 1) // 修改志愿
                {
                    course();
                    System.out.println("您的原志愿一为：" +student.getFirstVol());
                    System.out.println("您想修改志愿一为：(不想修改请输入”same“)");
                    String first = scanner.next();
                    if(first.equals("same"))
                    {
                        updateStu.setFirstVol(student.getFirstVol());
                    }
                    else
                    {
                        boolean b = isCourse(first);
                        if(b)
                            updateStu.setFirstVol(first);
                        else
                        {
                            System.out.println("课程名称不正确或课程已有助教！");
                            int t = tool(student_id);
                            if(t==0) return 0;
                        }
                    }
                    System.out.println();
                    System.out.println("您的原志愿二为：" +student.getSecondVol());
                    System.out.println("您想修改志愿二为：(不想修改请输入”same“)");
                    String second = scanner.next();
                    if(second.equals("same"))
                    {
                        updateStu.setSecondVol(student.getSecondVol());
                    }
                    else {
                        boolean b = isCourse(second);
                        if(b)
                            updateStu.setSecondVol(second);
                        else
                        {
                            System.out.println("课程名称不正确或课程已有助教！");
                            int t = tool(student_id);
                            if(t==0) return 0;
                        }
                    }
                    updateStu.setStudent_id(student_id);
                    studentDAO.updateStudent(updateStu);
                    System.out.println("修改成功，请等待审核");
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
                else if(choose == 2)  //查看所报课程的详细信息
                {
                    lookCourse(student.getFirstVol());
                    System.out.println();
                    lookCourse(student.getSecondVol());
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
                else if(choose == 3)
                {
                    return 0;
                }
                else
                {
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
            }
            else if(flag == 3) // 俩志愿都没填
            {
                System.out.println("检测到您还没有填报助教志愿！,输入1进入志愿填报（选择助教课程）");
                Scanner scanner = new Scanner(System.in);
                int a = scanner.nextInt();
                if(a == 1)
                {
                    course(); // 输出可选课程
                    Student updateStu = new Student();
                    updateStu.setStudent_id(student_id);
                    System.out.println("您的志愿一课程名称为：(弃选请输入null)");
                    String first = scanner.next();
                    if(first.equals("null"))
                    {
                        System.out.println("弃选志愿一！");
                    }
                    else {
                        boolean b = isCourse(first);
                        if(b)
                            updateStu.setFirstVol(first);
                        else
                        {
                            System.out.println("课程名称不正确或课程已有助教！志愿一暂时存为null");
                            updateStu.setFirstVol("null");
                        }

                    }


                    System.out.println("您的志愿二课程名称为：(弃选请输入null)");
                    String second = scanner.next();
                    if(second.equals("null"))
                    {
                        System.out.println("弃选志愿二！");
                    }
                    else {
                        boolean b1 = isCourse(second);
                        if(b1)
                            updateStu.setSecondVol(second);
                        else
                        {
                            System.out.println("课程名称不正确或课程已有助教！志愿二暂时存为null");
                            updateStu.setSecondVol("null");
                        }

                    }
                    studentDAO.updateStudent(updateStu);
                    System.out.println("填报成功，请等待审核");
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
                else
                {
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
            }
            else if(flag == 2)   // 志愿二没填
            {
                System.out.println("检测到您已填报志愿一，但未填报志愿二！");
                System.out.println("您的志愿一为：" +student.getFirstVol());
                System.out.println("输入1进入志愿填报（选择助教课程）");
                System.out.println();
                Scanner scanner = new Scanner(System.in);
                int a = scanner.nextInt();
                if(a == 1)
                {
                    course();
                    System.out.println("请选择您的志愿二课程：(不想选了请输入null)");
                    String second = scanner.next();
                    if(second.equals("null"))
                    {
                        int t = tool(student_id);
                        if(t==0) return 0;
                    }
                    Student updateStu = new Student();
                    updateStu.setStudent_id(student_id);
                    updateStu.setFirstVol(student.getFirstVol());
                    boolean b = isCourse(second);
                    if(b)
                        updateStu.setSecondVol(second);
                    else
                    {
                        System.out.println("课程名称不正确或课程已有助教！");
                        int t = tool(student_id);
                        if(t==0) return 0;
                    }
                    studentDAO.updateStudent(updateStu);
                    System.out.println("填报成功，请等待审核");
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
                else
                {
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
            }
            else if(flag == 1) // 志愿一没填
            {
                System.out.println("检测到您已填报志愿二，但未填报志愿一!");
                System.out.println("您的志愿二为：" +student.getSecondVol());
                System.out.println("输入1进入志愿填报（选择助教课程）");
                System.out.println();
                Scanner scanner = new Scanner(System.in);
                int a = scanner.nextInt();
                if(a == 1)
                {
                    course();
                    System.out.println("请选择您的志愿一课程：(不想选了请输入null)");
                    String first = scanner.next();
                    if (first.equals("null"))
                    {
                        int t = tool(student_id);
                        if(t==0) return 0;
                    }
                    Student updateStu = new Student();
                    updateStu.setStudent_id(student_id);

                    boolean b = isCourse(first);
                    if(b)
                        updateStu.setFirstVol(first);
                    else
                    {
                        System.out.println("课程名称不正确或课程已有助教！");
                        int t = tool(student_id);
                        if(t==0) return 0;
                    }
                    updateStu.setSecondVol(student.getSecondVol());
                    studentDAO.updateStudent(updateStu);
                    System.out.println("填报成功，请等待审核");
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
                else
                {
                    int t = tool(student_id);
                    if(t==0) return 0;
                }
            }
        }
        else if(nowState.equals("助教中"))
        {
            System.out.println("检测到您已有助教课程："+student.getCourse());
            System.out.println("请选择您要进行的操作：");
            System.out.println("1.查看助教课程具体信息");
            System.out.println("2.申请研究生助教工作评定");
            System.out.println("3.查看已填工作评定表");
            System.out.println("4.退出系统");
            Evaluate evaluate = evaluateDAO.getEvaluate(student_id);

            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            if(n == 1)
            {
                String course_name = student.getCourse();
                lookCourse(course_name);
                int m = middle(student_id);
                if(m==0) return 0;
            }
            else if(n == 2)
            {
                if(evaluate != null)
                {
                    System.out.println("经检测您已填写工作评定表，请勿重新填写！");
                    middle(student_id);
                }
                else
                {
                    StudentEvaluate se = new StudentEvaluate();
                    se.studentEvaluate(student);
                    int m = middle(student_id);
                    if(m==0) return 0;
                }
            }
            else if(n == 3)
            {
                if(evaluate == null)
                {
                    System.out.println("经检测您还未填写工作评定表，无法查看，请先填写评定表！");
                    int m = middle(student_id);
                    if(m==0) return 0;
                }
                else {
                    StudentEvaluate studentEvaluate = new StudentEvaluate();
                    studentEvaluate.lookEvaluate(evaluate);
                    int m = middle(student_id);
                    if(m==0) return 0;
                }
            }
            else if(n == 4)
                return 0;
            else
            {
                int m = middle(student_id);
                if(m==0) return 0;
            }
        }
        else if(nowState.equals("已结束"))
        {
            System.out.println("恭喜您已经通过了研究生助教工作评定，您的助教工作已完成，请准备毕业答辩吧！");
        }
        return 0;
    }
    public void course()
    {
        List<Course> courseList = courseDAO.getCourse();
        for(int i = 0;i < courseList.size();i++)
        {
            Course course = courseList.get(i);
            System.out.print("课程名："+course.getCourse_name());
            System.out.print("  授课教师："+course.getCourse_teacher());
            System.out.print("  课程性质："+course.getCourse_property());
            System.out.print("  课程对象："+course.getCourse_student());
            System.out.print("  学时数："+course.getCourse_hour());
            System.out.print("  选课人数："+course.getNumber());
            System.out.print("  课程状态："+course.getCourse_state());
            System.out.println();
        }
    }
    public int tool(String student_id)
    {
        System.out.println("请确认下面的操作：");
        System.out.println("若需要进行志愿填报、查看、修改，请输入1");
        System.out.println("若需要退出系统，请输入除1的其他任意数");
        Scanner scanner = new Scanner(System.in);
        int s = scanner.nextInt();
        if(s == 1)
        {
            chooseCourse(student_id);
        }
        else
            return 0;
        return 0;
    }
    public int middle(String student_id)
    {
        System.out.println("请确认下面的操作：");
        System.out.println("若需要进行评定表的申请、查看，请输入1");
        System.out.println("若需要退出系统，请输入除1的其他任意数");
        Scanner scanner = new Scanner(System.in);
        int s = scanner.nextInt();
        if(s == 1)
        {
            chooseCourse(student_id);
        }
        else
            return 0;
        return 0;
    }
    public void lookCourse(String course_name)
    {
        Course course = courseDAO.look(course_name);
        System.out.print("课程名："+course.getCourse_name());
        System.out.print("  授课教师："+course.getCourse_teacher());
        System.out.print("  课程性质："+course.getCourse_property());
        System.out.print("  课程对象："+course.getCourse_student());
        System.out.print("  学时数："+course.getCourse_hour());
        System.out.print("  选课人数："+course.getNumber());
        System.out.print("  课程状态："+course.getCourse_state());
        System.out.println();
    }
    public boolean isCourse(String course_name)
    {
        List<String> nameList = courseDAO.getCourseName();
        int flag = 0;
        for (int i = 0;i < nameList.size();i++)
        {
            if(nameList.get(i).equals(course_name))
            {
                flag = 1;
                break;
            }
        }
        if(flag == 1)
            return true;
        else
            return false;
    }
}
